var ACTION_ADD = "add";
var ACTION_EDIT = "edit";
var SUPPLIER_CATEGORY = "supplierCategory";
var SUPPLIER = "supplier";
var DISH = "dish";
var DISH_CATEGORY = "dishCategory";
var DISH_BUNDDLE = "dishBunddle";
var BUNDDLE_ITEM = "bunddleItem";
var ADS = "ads";
var COUPON = "coupon";
var MEMBER = "member";
var FEEDBACK = "feedback";

var website;
$(function() {
	jQuery.support.cors = true;
	website = $("#website").val();
	var tab = $("#currentTab").val();
	if (tab == SUPPLIER_CATEGORY) {
		initSupplierCategory();
	} else if (tab == SUPPLIER) {
		initSupplier();
	} else if (tab == ADS) {
		initAds();
	} else if (tab == COUPON) {
		initCoupon();
	} else if (tab == MEMBER) {
		initMember();
	} else if (tab == FEEDBACK) {
		initFeedback();
	}
});

function changeSelectAll(obj, className) {
	if ($(obj).attr("checked")) {
		$("." + className).each(function() {
			$(this).attr("checked", true);
		});
	} else {
		$("." + className).each(function() {
			$(this).attr("checked", false);
		});
	}
}

function deleteDialog(type,className) {
	var ids = getSelectedItems(className);
	if (ids == "") {
		alert("请至少选择一个选项");
		return;
	}
	$("#dialog-confirm").css("display", "inline");
	$("#dialog-confirm").dialog({
		resizable : false,
		height : 140,
		modal : true,
		buttons : {
			"确定删除" : function() {
				deleteAll(ids, type);
				$(this).dialog("close");
			},
			"取消" : function() {
				$(this).dialog("close");
			}
		}
	});
}

function getSelectedItems(className) {
	var ids = "";
	$("." + className).each(function() {
		if ($(this).attr("checked")) {
			if (ids == "") {
				ids = $(this).attr("key");
			} else {
				ids = ids + "," + $(this).attr("key");
			}
		}
	});
	return ids;
}

function getSelectdItemLength(className) {
	return getSelectedItems(className).split(",").length;
}

function openDeleteDialog(type,className,selectedId) {
	var ids = null;
	if(selectedId){
		ids = selectedId;
	}else{
		ids = getSelectedItems(className);
		if (ids == "") {
			alert("请至少选择一个选项");
			return;
		}
	}
	$("#dialog-confirm").css("display", "inline");
	$("#dialog-confirm").dialog({
		resizable : false,
		height : 140,
		modal : true,
		buttons : {
			"确定删除" : function() {
				deleteAll(ids, type);
				$(this).dialog("close");
			},
			"取消" : function() {
				$(this).dialog("close");
			}
		}
	});
}

function deleteAll(ids, type) {
	var params = {
		ids : ids
	}
	if (type == SUPPLIER_CATEGORY) {
		url = website + "boSupplierCategory/deleteAll";
	} else if (type == SUPPLIER) {
		url = website + "boSupplier/deleteAll";
	} else if (type == DISH) {
		url = website + "boDish/deleteAll";
	} else if (type == DISH_CATEGORY){
		url = website + "boDishCategory/deleteAll";
	} else if (type == DISH_BUNDDLE){
		url = website + "boBunddle/deleteAll";
	} else if (type == BUNDDLE_ITEM){
		url = website + "boBunddleItem/deleteAll";
	} else if (type == ADS){
		url = website + "boAds/deleteAll";
	} else if (type == COUPON){
		url = website + "boCoupon/deleteAll";
	} else if (type == MEMBER){
		url = website + "boMember/deleteAll";
	} else if (type == FEEDBACK){
		url = website + "boFeedback/deleteAll";
	}
	mask.open(true);
	ajaxPost(url, params, function(result) {
		$("#dialog-message").html(result);
		$("#dialog-message").dialog({
			modal : true,
			resizable : false,
			buttons : {
				Ok : function() {
					if (type == SUPPLIER_CATEGORY) {
						showSupplierCategory();
					} else if (type == SUPPLIER) {
						showSupplier();
					} else if (type == DISH) {
						showDish();
					} else if (type == DISH_CATEGORY) {
						showDishCategory();
					} else if (type == DISH_BUNDDLE) {
						showBunddle();
					} else if (type == BUNDDLE_ITEM) {
						showBunddleItem();
					} else if (type == ADS) {
						showAds();
					} else if (type == COUPON) {
						showCoupon();
					} else if (type == MEMBER) {
						showMember();
					} else if (type == FEEDBACK) {
						showFeedback();
					}
					$(this).dialog("close");
				}
			}
		});
		mask.close();
	}, "html");
}

function removeCopyFlag(type,id){
	var params = {
		id : id,
		type :type
	}
	if (type == DISH) {
		url = website + "boDish/removeCopyFlag";
	} else if (type == DISH_CATEGORY) {
		url = website + "boDishCategory/removeCopyFlag";
	}
	mask.open(true);
	ajaxPost(url, params, function(result) {
		if (type == DISH) {
			showDish();
		} else if (type == DISH_CATEGORY) {
			showDishCategory();
		}
		mask.close();
	}, "html");
}

function ajaxGet(url, params, callback, dataType) {
	$.ajax({
		url : url,
		data : params,
		success : callback,
		dataType : dataType,
		error : function(result, textStatus, e) {
			alert(result.responseText);
			mask.close();
		}
	});
}

function ajaxPost(url, params, callback, dataType) {
	$.ajax({
		url : url,
		data : params,
		type: "POST",
		success : callback,
		dataType : dataType,
		error : function(result, textStatus, e) {
			alert(result.responseText);
			mask.close();
		}
	});
}
